//
//  HomeViewController.m
//  VideoPic
//
//  Created by apple on 16/3/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HomeViewController.h"
#import "VideoCollectionCell.h"
#import "GHJokeCollectionViewCell.h"
#import "GHPicCollectionCell.h"
#import "YMPrimeCollectionViewCell.h"

@interface HomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic ,retain)UICollectionView *collection;
@property (nonatomic ,retain)UIScrollView *scroll;
@property (nonatomic ,retain)NSString *min_time;
@property (nonatomic ,retain)NSMutableArray *arrayData;

@end

@implementation HomeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
    [self createScrollView];
    
    
}



- (void)createScrollView {
    
    NSArray *arrTitle = @[@"视频",@"图片",@"段子",@"精华"];
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    [self.view addSubview:self.scroll];
    self.scroll.backgroundColor = [UIColor grayColor];
    self.scroll.contentSize = CGSizeMake(SCREEN_WIDTH, 0);
    
    for (int i = 0; i < 4; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((SCREEN_WIDTH /4) * i, 0, SCREEN_WIDTH /4, 40);
        button.backgroundColor = [UIColor colorWithRed:arc4random()% 254/255.f green:arc4random() % 254/255.f blue:arc4random() %254 /255.0 alpha:1];
        [self.scroll addSubview:button];
        [button setTitle:arrTitle[i] forState:UIControlStateNormal];

        button.tag = i + 1;
        
        [button addTarget:self action:@selector(moveItemWithButton:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)moveItemWithButton:(UIButton *)button {

    NSIndexPath *index = [NSIndexPath indexPathForItem:button.tag - 1 inSection:0];
     [self.collection scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

}




- (void)createCollection {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    //collection
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collection.pagingEnabled = YES;
    self.collection.scrollEnabled = YES;

    [self.view addSubview:self.collection];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    [self.collection registerClass:[VideoCollectionCell class] forCellWithReuseIdentifier:@"pool"];
    [self.collection registerClass:[GHJokeCollectionViewCell class] forCellWithReuseIdentifier:@"pool1"];
    [self.collection registerClass:[GHPicCollectionCell class] forCellWithReuseIdentifier:@"pool2"];
    [self.collection registerClass:[YMPrimeCollectionViewCell class] forCellWithReuseIdentifier:@"CellForPrime"];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 2) {
        GHJokeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.row == 1){
        GHPicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool2" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.row == 3){
        YMPrimeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellForPrime" forIndexPath:indexPath];
        return cell;
    }else {    
    
    VideoCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random()% 254/255.f green:arc4random() % 254/255.f blue:arc4random() %254 /255.0 alpha:1];
    
        return cell;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
